<?php

/**
 * Created by PhpStorm.
 * User: ChenXu
 * Date: 2017-05-17
 * Time: 16:35
 */
class api_coupon
{
    private $_mid;
    private $_G;
    private $_openid;
    private $_memberId;

    function __construct(){
        global $_G;
        $this->_G = &$_G;
        $this->_mid = $_GET['mid'];
//        $this->_openid = $this->_G[$this->_mid]['openid'];
//        $this->_memberId = $this->_G[$this->_mid]['memberid'];
        $clientkey = getcookie('HeaderKey_' . $this->_mid);
        //POst过来的用户信息
        $clientkey = $clientkey ? $clientkey : $_GET['HeaderKey'];
        //解密用户信息
        $Token = getcookie("Token");
        $app = getcookie("third_party_wxweb");
        $this->_app= $app;
        list($auType, $openid, $mid, $subscribe) = explode("\t", SING_auth($clientkey, 'DECODE', md5(getglobal('cookiepre', 'config/cookie') . $Token)));

        if($app=='app'){
            $_G[$this->_mid]['memberid']=$this->_memberId= $openid;
            DB::SetMainDB(DB_MAIN);
            if($this->_memberId){
                $rs = DB::fetch_first(" select openid from %t  where display=1 and id=%d", array("common_member", $this->_memberId));
                $_G[$this->_mid]['openid']=$this->_openid= $rs['openid'];
            }
        }else{
            $_G[$this->_mid]['openid']=$this->_openid= $openid;
            DB::SetMainDB(DB_MAIN);
            if($this->_openid){
                $rs = DB::fetch_first(" select id from %t  where display=1 and openid=%s", array("common_member", $this->_openid));
                $_G[$this->_mid]['memberid']=$this->_memberId= $rs['id'];
            }
        }
    }

    /**
     * 获取奖券信息
     * @return array
     */
    function getMyCoupon(){
        $where="";
        if($this->_memberId && $this->_openid){
            $where.=DB::format("and member_id=%d and openid=%s",array($this->_memberId,$this->_openid));
        }elseif($this->_memberId){
            $where.=DB::format("and member_id=%d",array($this->_memberId));
        }elseif($this->_openid){
            $where.=DB::format("and openid=%s",array($this->_openid));
        }else{
            return array("Error"=>101,"Msg"=>"请先登录");
        }
        $sql=DB::format("select id,title,price,limit_btime,limit_etime,is_verify from %t where display=1 and is_activity=1 %i",array("es_coupon_log",$where));
        $coupons=DB::fetch_all($sql);
    }
    public function coupon_verify_batch(){
        $account = $this->_G['g_verify_id'];
        DB::SetMainDB(DB_ESHOP);
        $verify_id = DB::result_first("select member_id from %t where account=%s ",array("es_shake_verify_account",$account));
        if(!$verify_id){
            return array("Error" => 401, "Msg" => "账号错误");
        }
//        $log_ids = $this->_G['g_ids'];
//        $data = explode(",", $log_ids);
        $data = $this->_G['g_ids'];
        if(!$this->_openid && !$this->_memberId){
            return array("Error" => 400, "Msg" => "请先登录");
        }
        foreach ($data as $k=>$v){
            DB::SetMainDB(DB_ESHOP);
            $outletUser = DB::fetch_first("select * from %t where memberid=%d and display=1 and status=1 and mid=%s",array("es_outlet_user",$verify_id,$this->_mid));
            if(!$outletUser){
                return array("Error" => 2197, "Msg" => "核销账号错误");
            }
            DB::SetMainDB(DB_ESHOP);
            $couponLog = DB::fetch_first("select id,order_id,coupon_id,limit_btime,limit_etime,status,display,is_verify,`type`,gettype,store_id,member_id from %t where id=%d and mid=%s",array("es_coupon_log",$v['id'],$this->_mid));

            DB::SetMainDB(DB_ESHOP);
            $outlet = DB::fetch_first("select * from %t where coupon_id=%d and mid=%s",array("es_coupon_distance",$couponLog['coupon_id'],$this->_mid));


            if($outlet['outlet_id']!=$outletUser['outletid']){
                return array("Error" => 2198, "Msg" => "门店无核销权限");
            }
            /*
             * author shiyu
             * 添加了针对卡券所属人的校验
             * 2017年5月2日15:22:45
             * */
            $uniid = $this->_G['g_uuid'];//获取卡券生成二维码时候的生成者的uuid//二次修改规则 比对 memberid 为了应对openid unionid为空的情况
            //通过卡券领用表的member_id获取实际领用人的uuid

            if($couponLog['member_id']!=$this->_memberId) {
                return array("Error" => 2199, "Msg" => "卡券所属人和登录人身份不一致");
            }
            if($couponLog['member_id'] != $this->_memberId){
                $errInsert['p_memberid']=$uniid;
                $errInsert['c_memberid']=$couponLog['member_id'];
                $errInsert['coupon_log_id']=$couponLog['id'];
                $errInsert['inputtime']=TIME;
                DB::SetMainDB(DB_ESHOP);
                DB::insert("es_coupon_verify_err", $errInsert);
//            return array("Error" => 2299, "Msg" => "卡券所属人和当前使用人的身份不一致",'data'=>$uuid['unionid']);
            }

            /*
             * 修改结束
             * */
            if($couponLog['display']!=1){
                return array("Error"=>2101,"Msg"=>"该优惠券已失效");
            }

            if($couponLog['type']==1){
                return array("Error"=>2104,"Msg"=>"线上优惠券不能被核销");
            }

            if($couponLog['status']!=1){
                if($couponLog['status']==2){
                    return array("Error"=>2102,"Msg"=>"该优惠券已经核销过了");
                }else{
                    return array("Error"=>2103,"Msg"=>"该优惠券已失效");
                }
            }

            if($couponLog['is_verify']){
                return array("Error"=>306,"Msg"=>"该优惠券已经核销过了");
            }

            if($couponLog['limit_btime']>TIME){
                return array("Error"=>307,"Msg"=>"该优惠券还未到使用时间");
            }
            if($couponLog['limit_etime']<TIME){
                return array("Error"=>308,"Msg"=>"该优惠券已经过期了");
            }

            $insert = array();
            $insert['inputtime'] = TIME;
            $insert['mid'] = $this->_mid;
            $insert['member_id'] = $verify_id;
            $insert['coupon_verify_member_id'] = $verify_id;
            $insert['outlet_id'] = $outlet['outlet_id'];
            $insert['store_id'] = $couponLog['store_id'];
            $insert['coupon_log_id'] = $v['id'];
            $insert['coupon_id'] = $couponLog['coupon_id'];
            $insert['verify_openid'] = $this->_openid;
            $insert['type'] = 'ewm';

            DB::SetMainDB(DB_ESHOP);
            DB::insert("es_coupon_verify_log", $insert);
            if($couponLog['order_id']){
                DB::SetMainDB(DB_ESHOP);
                $service_charge = DB::fetch_first("SELECT service_charge FROM %t WHERE id=%s",array("es_coupon_order",$couponLog['order_id']));
                if($service_charge['service_charge'] < 0){
                    DB::SetMainDB(DB_ESHOP);
                    $settlementid = DB::fetch_first("SELECT settlement FROM %t WHERE id=%d",array("es_outlet",$insert['outlet_id']));
                    if($settlementid['settlement'] <=0){
                        $upt['service_charge'] = 0;
                    }else{
                        DB::SetMainDB(DB_ESHOP);
                        $service_charge = DB::fetch_first("SELECT service_charge FROM %t WHERE id=%d",array("es_settlement",$settlementid['settlement']));
                        $upt['service_charge'] = $service_charge['service_charge'];
                    }
                    DB::update("es_coupon_order",$upt,array("mid"=>$this->_mid,"id"=>$couponLog['order_id']));
                }
            }

            DB::SetMainDB(DB_ESHOP);
            DB::update("es_coupon_log", array("updatetime"=>TIME,"status"=>2,"is_verify"=>1),array("mid"=>$this->_mid,"id"=>$v['id']));
        }
        return array("Error"=>0,"Msg"=>"核销成功");

    }
}